<template>
  <page-layout background="transparent">
    <div slot="header"
         class="header">
      <div class="left">
        <div class="avatar"
             :style="{backgroundImage: `url(${$store.state.user.avatar})`}" />
        <div class="greeting">
          <div class="greeting-msg">{{greeting}}</div>
          <div>
            <iframe scrolling="no"
                    src="https://tianqiapi.com/api.php?style=tz&skin=grape&color=515a6e&fontsize=14"
                    frameborder="0"
                    width="100%"
                    height="20"
                    allowtransparency="true"></iframe>
          </div>
        </div>
      </div>
      <div class="right">
        <div class="item"
             v-has-role="['ROLE_SALEMAN', 'ROLE_MANAGER']">
          <div>
            <span class="icon">
              <Icon type="xiangmu" />
            </span>
            <span>项目数</span>
          </div>
          <div class="value">{{info.projectCount}}</div>
        </div>
        <div class="item"
             v-has-role="['ROLE_SALEMAN']">
          <div>
            <span class="icon">
              <Icon type="hetong" />
            </span>
            <span>合同草稿</span>
          </div>
          <div class="value">{{info.draftCount}}</div>
        </div>
        <div class="item"
             v-has-role="['ROLE_SALEMAN', 'ROLE_TREASURER']">
          <div>
            <span class="icon">
              <Icon type="shouju" />
            </span>
            <span>待回收收据</span>
          </div>
          <div class="value">{{info.receiptNotRecycleCount}}</div>
        </div>
        <div class="item"
             v-has-role="['ROLE_SALEMAN', 'ROLE_TREASURER']">
          <div>
            <span class="icon">
              <Icon type="fapiao" />
            </span>
            <span>待回收发票</span>
          </div>
          <div class="value">{{info.invoiceNotRecycleCount}}</div>
        </div>
      </div>
    </div>
    <div>
      <!-- 管理员 -->
      <div class="info-list"
           v-has-role="['ROLE_BUSINESS_SYSTEM_ADMIN']">
        <Row :gutter="20">
          <i-col :span="12"
                 :sm="12"
                 :xl="6">
            <div class="item">
              <div class="icon">
                <Icon type="stick-man-2"
                      size="30" />
              </div>
              <div class="content">
                <div class="value">{{info.systemUserCount}}</div>
                <div class="label">系统用户数</div>
              </div>
            </div>
          </i-col>
          <i-col :span="12"
                 :sm="12"
                 :xl="6">
            <div class="item">
              <div class="icon">
                <Icon type="stick-man-1"
                      size="30" />
              </div>
              <div class="content">
                <div class="value">{{info.systemActiveCount}}</div>
                <div class="label">活跃用户数</div>
              </div>
            </div>
          </i-col>
          <i-col :span="12"
                 :sm="12"
                 :xl="6">
            <div class="item">
              <div class="icon">
                <Icon type="xiangmu"
                      size="30" />
              </div>
              <div class="content">
                <div class="value">{{info.projectTotalCount}}</div>
                <div class="label">项目总数</div>
              </div>
            </div>
          </i-col>
          <i-col :span="12"
                 :sm="12"
                 :xl="6">
            <div class="item">
              <div class="icon">
                <Icon type="hetong"
                      size="30" />
              </div>
              <div class="content">
                <div class="value">{{info.contractTotalCount}}</div>
                <div class="label">合同总数</div>
              </div>
            </div>
          </i-col>
        </Row>
      </div>
      <!-- 业务经理 -->
      <div class="info-list"
           v-has-role="['ROLE_MANAGER']">
        <Row :gutter="20">
          <i-col :span="12"
                 :sm="12"
                 :xl="6">
            <div class="item">
              <div class="icon">
                <Icon type="xiangmu"
                      size="30" />
              </div>
              <div class="content">
                <div class="value">{{info.teamProjectTotalCount}}</div>
                <div class="label">总项目数量</div>
              </div>
            </div>
          </i-col>
          <i-col :span="12"
                 :sm="12"
                 :xl="6">
            <div class="item">
              <div class="icon">
                <Icon type="hetong"
                      size="30" />
              </div>
              <div class="content">
                <div class="value">{{info.teamContractTotalCount}}</div>
                <div class="label">总合同数</div>
              </div>
            </div>
          </i-col>
          <i-col :span="12"
                 :sm="12"
                 :xl="6">
            <div class="item">
              <div class="icon">
                <Icon type="saleman"
                      size="30" />
              </div>
              <div class="content">
                <div class="value">{{info.teamSaleManCount}}</div>
                <div class="label">业务员数量</div>
              </div>
            </div>
          </i-col>
          <i-col :span="12"
                 :sm="12"
                 :xl="6">
            <div class="item">
              <div class="icon">
                <Icon type="money-1"
                      size="30" />
              </div>
              <div class="content">
                <div class="value">{{info.teamProjectMoney | number(2)}}</div>
                <div class="label">业务总金额</div>
              </div>
            </div>
          </i-col>
        </Row>
      </div>
      <!-- 业务员 -->
      <div class="info-list"
           v-has-role="['ROLE_SALEMAN']">
        <Row :gutter="20">
          <i-col :span="12"
                 :sm="12"
                 :xl="6">
            <div class="item">
              <div class="icon">
                <Icon type="stick-man-2"
                      size="30" />
              </div>
              <div class="content">
                <div class="value">{{info.selfCustomerCount}}</div>
                <div class="label">客户数量</div>
              </div>
            </div>
          </i-col>
          <i-col :span="12"
                 :sm="12"
                 :xl="6">
            <div class="item">
              <div class="icon">
                <Icon type="xiangmu"
                      size="30" />
              </div>
              <div class="content">
                <div class="value">{{info.selfProjectCount}}</div>
                <div class="label">项目数量</div>
              </div>
            </div>
          </i-col>
          <i-col :span="12"
                 :sm="12"
                 :xl="6">
            <div class="item">
              <div class="icon">
                <Icon type="hetong"
                      size="30" />
              </div>
              <div class="content">
                <div class="value">{{info.selfContractCount}}</div>
                <div class="label">合同数量</div>
              </div>
            </div>
          </i-col>
          <i-col :span="12"
                 :sm="12"
                 :xl="6">
            <div class="item">
              <div class="icon">
                <Icon type="money-1"
                      size="30" />
              </div>
              <div class="content">
                <div class="value">{{info.selfProjectMoney | number(2)}}</div>
                <div class="label">业务总金额</div>
              </div>
            </div>
          </i-col>
        </Row>
      </div>
      <!-- 财务 -->
      <div class="info-list"
           v-has-role="['ROLE_TREASURER']">
        <Row :gutter="20">
          <i-col :span="12"
                 :sm="12"
                 :xl="6">
            <div class="item">
              <div class="icon">
                <Icon type="hetong"
                      size="30" />
              </div>
              <div class="content">
                <div class="value">{{info.monthContractCount}}</div>
                <div class="label">合同数量（本月）</div>
              </div>
            </div>
          </i-col>
          <i-col :span="12"
                 :sm="12"
                 :xl="6">
            <div class="item">
              <div class="icon">
                <Icon type="money-1"
                      size="30" />
              </div>
              <div class="content">
                <div class="value">{{info.monthProjectMoney}}</div>
                <div class="label">业务总金额（本月）</div>
              </div>
            </div>
          </i-col>
          <i-col :span="12"
                 :sm="12"
                 :xl="6">
            <div class="item">
              <div class="icon">
                <Icon type="money-2"
                      size="30" />
              </div>
              <div class="content">
                <div class="value">{{info.monthRefundCount | number(2)}}</div>
                <div class="label">退款金额（本月）</div>
              </div>
            </div>
          </i-col>
          <i-col :span="12"
                 :sm="12"
                 :xl="6">
            <div class="item">
              <div class="icon">
                <Icon type="money-3"
                      size="30" />
              </div>
              <div class="content">
                <div class="value">{{info.monthInvoiceCount}}</div>
                <div class="label">开票金额（本月）</div>
              </div>
            </div>
          </i-col>
        </Row>
      </div>
    </div>

    <div class="margin-t-10"
         v-has-role="['ROLE_SALEMAN']">
      <Card shadow>
        <p slot="title">近半年业务变化</p>
        <ve-histogram :data="chartDataSaleMan"
                      :settings="chartSettings"
                      :extend="extend"></ve-histogram>
      </Card>
    </div>
    <div class="margin-t-10"
         v-has-role="['ROLE_MANAGER']">
      <Card shadow>
        <p slot="title">近半年业务变化（片区）</p>
        <ve-histogram :data="chartDataManager"
                      :settings="chartSettings"
                      :extend="extend"></ve-histogram>
      </Card>
    </div>
  </page-layout>
</template>

<script>
import {
  getAdminStatisicInfo,
  getManagerStatisicInfo,
  getSalemanStatisicInfo,
  getTreasurerStatisicInfo,
  getSalemanHistory,
  getManagerHistory
} from './service'
export default {
  components: {},
  created () {
    let hours = new Date().getHours()
    switch (true) {
      case hours >= 0 && hours < 4: {
        this.greeting = `夜深了，${
          this.$store.state.user.name
        }，早睡早起身体好！`
        break
      }
      case hours >= 4 && hours < 6: {
        this.greeting = `早安，${this.$store.state.user.name}，黎明，就在眼前！`
        break
      }
      case hours >= 6 && hours < 9: {
        this.greeting = `早安，${
          this.$store.state.user.name
        }，今天又是元气满满的一天！`
        break
      }
      case hours >= 9 && hours < 12: {
        this.greeting = `早安，${
          this.$store.state.user.name
        }，开始您一天的工作吧！`
        break
      }
      case hours >= 12 && hours < 13: {
        this.greeting = `中午好，${
          this.$store.state.user.name
        }，努力工作的你，别忘记吃午餐哦！`
        break
      }
      case hours >= 13 && hours < 14: {
        this.greeting = `中午好，${
          this.$store.state.user.name
        }，休息一下，休息是为了更好地工作！`
        break
      }
      case hours >= 14 && hours < 18: {
        this.greeting = `下午好，${
          this.$store.state.user.name
        }，来杯咖啡提提神！`
        break
      }
      case hours >= 18 && hours < 20: {
        this.greeting = `晚上好，${this.$store.state.user.name}，晚饭你吃了么！`
        break
      }
      case hours >= 20 && hours <= 24: {
        this.greeting = `晚上好，${
          this.$store.state.user.name
        }，还在加班的你最可敬！`
        break
      }
    }

    if (this.$store.state.user.roles.includes('ROLE_BUSINESS_SYSTEM_ADMIN')) {
      this.getAdminStatisicInfo()
    }
    if (this.$store.state.user.roles.includes('ROLE_MANAGER')) {
      this.getManagerStatisicInfo()
      this.getManagerHistory()
    }
    if (this.$store.state.user.roles.includes('ROLE_SALEMAN')) {
      this.getSalemanStatisicInfo()
      this.getSalemanHistory()
    }
    if (this.$store.state.user.roles.includes('ROLE_TREASURER')) {
      this.getTreasurerStatisicInfo()
    }
  },
  data () {
    return {
      greeting: `${this.$store.state.user.name} 您好！`,
      extend: {
        // showLine: ['签约数量']
        series: {
          label: { show: true, position: 'top' }
        }
      },
      chartSettings: {
        // showLine: ['签约数量']
        axisSite: { right: ['业务金额'] },
        yAxisType: ['KMB', 'KMB'],
        yAxisName: ['宗', '元']
      },
      chartDataSaleMan: {
        columns: ['日期', '签约数量', '业务金额'],
        rows: [
          // { 日期: '2019.01', 签约数量: 13, 业务金额: 1093 },
          // { 日期: '2019.02', 签约数量: 35, 业务金额: 3230 },
          // { 日期: '2019.03', 签约数量: 29, 业务金额: 2623 },
          // { 日期: '2019.04', 签约数量: 17, 业务金额: 1423 },
          // { 日期: '2019.05', 签约数量: 37, 业务金额: 3492 },
          // { 日期: '2019.06', 签约数量: 45, 业务金额: 4293 }
        ]
      },
      chartDataManager: {
        columns: ['日期', '签约数量', '业务金额'],
        rows: [
          // { 日期: '2019.01', 签约数量: 13, 业务金额: 1093 },
          // { 日期: '2019.02', 签约数量: 35, 业务金额: 3230 },
          // { 日期: '2019.03', 签约数量: 29, 业务金额: 2623 },
          // { 日期: '2019.04', 签约数量: 17, 业务金额: 1423 },
          // { 日期: '2019.05', 签约数量: 37, 业务金额: 3492 },
          // { 日期: '2019.06', 签约数量: 45, 业务金额: 4293 }
        ]
      },
      info: {
        projectCount: 0,
        draftCount: 0,
        receiptNotRecycleCount: 0,
        invoiceNotRecycleCount: 0,
        // 系统管理员
        systemUserCount: 0,
        systemActiveCount: 0,
        projectTotalCount: 0,
        contractTotalCount: 0,
        // 业务经理
        teamProjectTotalCount: 0,
        teamContractTotalCount: 0,
        teamSaleManCount: 0,
        teamProjectMoney: 0,
        // 业务员
        selfCustomerCount: 0,
        selfProjectCount: 0,
        selfContractCount: 0,
        selfProjectMoney: 0,
        // 财务
        monthContractCount: 0,
        monthProjectMoney: 0,
        monthRefundCount: 0,
        monthInvoiceCount: 0
      }
    }
  },
  methods: {
    getAdminStatisicInfo () {
      getAdminStatisicInfo().then(result => {
        if (result.isSuccess) {
          this.info.systemUserCount = result.data.systemUserCount
          this.info.systemActiveCount = result.data.systemActiveCount
          this.info.projectTotalCount = result.data.projectTotalCount
          this.info.contractTotalCount = result.data.contractTotalCount
        }
      })
    },
    getManagerStatisicInfo () {
      getManagerStatisicInfo().then(result => {
        if (result.isSuccess) {
          this.info.teamProjectTotalCount = result.data.teamProjectTotalCount
          this.info.projectCount = this.info.teamContractTotalCount =
            result.data.teamContractTotalCount
          this.info.teamSaleManCount = result.data.teamSaleManCount
          this.info.teamProjectMoney = result.data.teamProjectMoney
        }
      })
    },
    getSalemanStatisicInfo () {
      getSalemanStatisicInfo().then(result => {
        if (result.isSuccess) {
          this.info.selfCustomerCount = result.data.selfCustomerCount
          this.info.projectCount = this.info.selfProjectCount =
            result.data.selfProjectCount
          this.info.selfContractCount = result.data.selfContractCount
          this.info.draftCount = result.data.draftCount
          this.info.selfProjectMoney = result.data.selfProjectMoney
          this.info.receiptNotRecycleCount = result.data.receiptNotRecycleCount
          this.info.invoiceNotRecycleCount = result.data.invoiceNotRecycleCount
        }
      })
    },
    getTreasurerStatisicInfo () {
      getTreasurerStatisicInfo().then(result => {
        if (result.isSuccess) {
          this.info.monthContractCount = result.data.monthContractCount
          this.info.monthProjectMoney = result.data.monthProjectMoney
          this.info.monthRefundCount = result.data.monthRefundCount
          this.info.monthInvoiceCount = result.data.monthInvoiceCount
          this.info.receiptNotRecycleCount = result.data.receiptNotRecycleCount
          this.info.invoiceNotRecycleCount = result.data.invoiceNotRecycleCount
        }
      })
    },
    getSalemanHistory () {
      getSalemanHistory().then(result => {
        if (result.isSuccess) {
          this.chartDataSaleMan.rows.splice(
            0,
            this.chartDataSaleMan.rows.length,
            ...result.data
          )
        }
      })
    },
    getManagerHistory () {
      getManagerHistory().then(result => {
        if (result.isSuccess) {
          this.chartDataManager.rows.splice(
            0,
            this.chartDataManager.rows.length,
            ...result.data
          )
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.header {
  display: flex;
  padding: 16px 32px;
  background: #fff;
  border-bottom: 1px solid #e8eaec;
  flex-wrap: wrap;
  .left {
    flex: 1;
    min-width: 500px;
    padding: 10px 0;
    .avatar {
      width: 64px;
      height: 64px;
      border-radius: 50%;
      display: inline-block;
      vertical-align: top;
      background-position: center;
      background-size: 100%;
    }
    .greeting {
      display: inline-block;
      vertical-align: top;
      margin-left: 16px;
    }
    .greeting-msg {
      font-size: 20px;
      font-weight: 700;
      margin-bottom: 12px;
    }
  }
  .right {
    min-width: 300px;
    text-align: right;
    padding: 10px 0;
    &::after {
      content: "";
      height: 100%;
      display: inline-block;
      vertical-align: middle;
    }
    .item {
      display: inline-block;
      text-align: left;
      vertical-align: middle;
      + .item {
        margin-left: 10px;
      }
      &:nth-of-type(1) {
        .icon {
          color: #40a9ff;
          background-color: #e6f7ff;
        }
      }
      &:nth-of-type(2) {
        .icon {
          color: #ffa940;
          background-color: #fff7e6;
        }
      }
      &:nth-of-type(3) {
        .icon {
          color: #73d13d;
          background-color: #f6ffed;
        }
      }
      &:nth-of-type(4) {
        .icon {
          color: #f9b4c4;
          background-color: #fef0f3;
        }
      }
      span {
        display: inline-block;
        border-radius: 50%;
        vertical-align: middle;
        &.icon {
          height: 21px;
          width: 21px;
          text-align: center;
        }
      }
      .value {
        font-size: 22px;
        text-align: right;
      }
    }
  }
}

.info-list {
  .item {
    background: #fff;
    border-radius: 5px;
    display: flex;
    padding: 16px 32px;
    margin-bottom: 16px;
    justify-content: center;
    .icon {
      &::after {
        content: "";
        display: inline-block;
        height: 100%;
        vertical-align: middle;
      }
    }
    .content {
      flex: 1;
      margin-left: 16px;
      overflow: hidden;
      text-align: center;
      .value {
        font-size: 32px;
        margin-top: 8px;
        line-height: 40px;
        height: 40px;
      }
      .label {
        font-size: 12px;
        color: #999;
        line-height: 20px;
        height: 20px;
      }
    }
  }
}

@media screen and (min-width: 1200px) and (max-width: 1400px) {
  .info-list {
    .item {
      .icon {
        display: none;
      }
    }
  }
}
// @media screen and (max-width: 576px) {
//   .info-list {
//     .item {
//       .icon {
//         display: none;
//       }
//     }
//   }
// }
</style>
